#include "settingswidget.h"

SettingsWidget::SettingsWidget(QWidget *parent) :
    QWidget(parent)
{
    QGridLayout *centralLayout = new QGridLayout();
    this->setLayout(centralLayout);


    QLabel *incrementalTresholdLabel = new QLabel(tr("Incremental treshold value: "));
    centralLayout->addWidget(incrementalTresholdLabel, 0, 0);

    incrementalTresholdValue = new QSpinBox();
    centralLayout->addWidget(incrementalTresholdValue, 0, 1);


    QLabel *tempLabel = new QLabel(tr("Temporary directory"));
    centralLayout->addWidget(tempLabel, 1, 0);

    tempValue = new QLineEdit();
    centralLayout->addWidget(tempValue, 1, 1);

    QPushButton *tempButton = new QPushButton(tr("..."));
    centralLayout->addWidget(tempButton, 1, 2);


    QLabel *maximalHashChunkLabel = new QLabel(tr("Maximum size of hashed chunk (MB)"));
    centralLayout->addWidget(maximalHashChunkLabel, 2, 0);

    maximalHashChunkValue = new QSpinBox();
    maximalHashChunkValue->setRange(1,500);
    centralLayout->addWidget(maximalHashChunkValue, 2, 1);

    QHBoxLayout *buttonLayout = new QHBoxLayout();
    centralLayout->addLayout(buttonLayout, 3, 0, 1, 3);

    QPushButton *saveButton = new QPushButton(tr("Save"));
    saveButton->setObjectName("genericButton");
    connect(saveButton, SIGNAL(clicked()), this, SLOT(onSaveButtonClicked()));
    buttonLayout->addWidget(saveButton);

    QPushButton *loadButton = new QPushButton(tr("Load"));
    loadButton->setObjectName("genericButton");
    connect(loadButton, SIGNAL(clicked()), this, SLOT(onLoadButtonClicked()));
    buttonLayout->addWidget(loadButton);

    centralLayout->setRowStretch(3, 5);
}

void SettingsWidget::onSaveButtonClicked()
{
    QSettings settings(this);
    settings.setValue("IncrementalTresholdValue", this->incrementalTresholdValue->value());
    settings.setValue("TemporaryDirectory", this->tempValue->text());
    settings.setValue("MaximalHashedChunk", this->maximalHashChunkValue->value());
}

void SettingsWidget::onLoadButtonClicked()
{
    QSettings settings(this);
    this->incrementalTresholdValue->setValue(settings.value("IncrementalTresholdValue", 1).toInt());
    this->tempValue->setText(settings.value("TemporaryDirectory", "C:/Temp").toString());
    this->maximalHashChunkValue->setValue(settings.value("MaximalHashedChunk", 100).toInt());
}
